package com.uzai.console.component.dingding;

import com.alibaba.fastjson.JSONObject;

import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import com.uzai.console.service.dingding.DingDingCallBackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class DingDingTalkEventUtils implements CommandLineRunner {

    private static Logger logger= LoggerFactory.getLogger(DingDingTalkEventUtils.class);

    @Autowired
    private DingDingCallBackService dingDingCallBackService;

    @Autowired
    private Environment environment;
    @Override
    public void run(String... args) throws Exception {

        //区分正式还是测试环境
        String[] activeProfiles = environment.getActiveProfiles();
        logger.info("activeProfiles:"+ Arrays.toString(activeProfiles));

        if(Arrays.asList(activeProfiles).contains("pro")){
            OpenDingTalkStreamClientBuilder
                    .custom()
                    .credential(new AuthClientCredential("dinggbqnqmkldsz7xr4f", "ENXv0vO7a5JZR1YKtp1jOhO3TkvVseTE62xOEbXb5ktwha9YSvsryD1PpwvE_ldc"))
                    //注册事件监听
                    .registerAllEventListener(new GenericEventListener() {
                        public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                            try {
                                //事件唯一Id
                                String eventId = event.getEventId();
                                //事件类型
                                String eventType = event.getEventType();
                                //事件产生时间
                                Long bornTime = event.getEventBornTime();

                                logger.info("eventId is {},eventType is {},bornTime is {}",eventId,eventType,bornTime);

                                if("chat_add_member".equals(eventType)){
                                    //获取事件体,  处理事件
                                    JSONObject bizData = event.getData();
                                    logger.info("bizData is {}",bizData.toJSONString());
                                    //扫码进入的，修改
                                    if(bizData.get("operator")!=null){
                                        String openConversationId = bizData.getString("openConversationId");
                                        dingDingCallBackService.warningGroupAddMember(openConversationId);
                                    }
                                }
                                //消费成功
                                return EventAckStatus.SUCCESS;
                            } catch (Exception e) {
                                //消费失败
                                return EventAckStatus.LATER;
                            }
                        }
                    })
                    .build().start();
        }
    }
}
